﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using condortravel.operaciones.business.Security;
using condortravel.operaciones.core.Security;
using condortravel.operaciones.common;
using System.Security.Principal;

namespace CondorTravelOperacionesWeb.Source
{
    
    public abstract class ControlBase: UserControl
    {
        #region Eventos personalizados del control de busqueda

        public delegate void EventOnErrorControl(object sender, Exception e);
        public abstract event EventOnErrorControl ErrorControl;

        public delegate void EventOnMessageControl(string message);
        public abstract event EventOnMessageControl MessageControl;

        #endregion

        #region Attributes
        #endregion

        #region Properties

        public string CultureName
        {
            get { return System.Threading.Thread.CurrentThread.CurrentCulture.Name; }

        }
        public UserLoggedIn User
        {
            get
            {
                return (UserLoggedIn)Session[AppStatic.SESSION_USERAUTHENTICATED];
            }
        }
        #endregion

        #region Business Object
        private SecurityServices securityService;

        public SecurityServices SecurityServices
        {
            get { return securityService; }
            set { securityService = value; }
        }
        #endregion

        #region Page's Events
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {

            checkAccess();
            base.OnInit(e);

        }
        #endregion

        #region Private methods
        /// <summary>
        /// Check the user's acces 
        /// </summary>
        private void checkAccess()
        {
            if (!this.IsUserAuthenticated())
            {
                //string appCode = ConfigurationManager.AppSettings["AppCode"];
                //UserLoggedIn user = securityService.getUserLoggedIn(appCode, Request.LogonUserIdentity.Name, this.CultureName);
                //if (user != null)
                //{

                //    Session[AppStatic.SESSION_USERAUTHENTICATED] = user;
                //    Session[AppStatic.SESSION_ISUSERAUTHENTICATED] = true;
                //}
            }
        }

        #endregion

        #region public methods
        public bool IsUserAuthenticated()
        {
            if (Session[AppStatic.SESSION_ISUSERAUTHENTICATED] == null)
                return false;
            return (bool)Session[AppStatic.SESSION_ISUSERAUTHENTICATED];
        }
        #endregion
    }
}
